var SCRIBEFIRE_HISTORY = {
	load : function () {
		try {
			// Delete the old file and filename.
			var oldFile = gPerformancingUtil.getFilePath("performancing_history.xml");
			PerFormancingFileIO.unlink(oldFile);
		} catch (alreadyDone) {
		}

		try {
			// Delete the old file and filename.
			var oldFile = gPerformancingUtil.getFilePath("performancing-pages.xml");
			PerFormancingFileIO.unlink(oldFile);
		} catch (alreadyDone) {
		}
	},
	
	saveItem : function (postId, content, title, categories, dateCreated, tags, isDraft, type, slug, custom_fields) {
		if (dateCreated) {
			dateCreated = new Date(dateCreated).getTime();
		}
		else {
			dateCreated = 0;
		}
		
		if (!custom_fields) {
			custom_fields = [];
		}
		
		var db = SCRIBEFIRE_DB.getDB();
		
		try {
			var del = db.createStatement("DELETE FROM "+SCRIBEFIRE_DB.historyTable+" WHERE postId=?1 AND blogId=?2");
			del.bindStringParameter(0, postId);
			del.bindInt32Parameter(1, SCRIBEFIRE_BLOG.id);
			del.execute();
		} catch (e) {
			SFLOG(e);
		}
		
		try {
			var insert = db.createStatement("INSERT INTO "+SCRIBEFIRE_DB.historyTable+" (postId, content, title, categories, dateCreated, tags, isDraft, type, blogId, slug, custom_fields) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)");
			insert.bindStringParameter(0, postId);
			insert.bindUTF8StringParameter(1, content);
			insert.bindUTF8StringParameter(2, title);
			insert.bindUTF8StringParameter(3, categories);
			insert.bindInt64Parameter(4, dateCreated);
			insert.bindUTF8StringParameter(5, tags);
			insert.bindInt32Parameter(6, (isDraft ? 1 : 0));
			insert.bindStringParameter(7, type);
			insert.bindInt32Parameter(8, SCRIBEFIRE_BLOG.id);
			insert.bindStringParameter(9, slug);
			insert.bindStringParameter(10, JSON.stringify(custom_fields));
			insert.execute();
		} catch (e) {
			SFLOG(e);
		}
		
		SCRIBEFIRE_DB.closeDB(db);
	},
	
	addHistoryItemToEditor : function (theElement, type) {
		// First check if the item is already open.
		if (!performancingEditor.selectTabForEntry(type, theElement.getAttribute("postid"))) {
			performancingEditor.newTab(true, SCRIBEFIRE_HISTORY.continueAddHistoryItemToEditor, [ theElement, type ]);
		}
	},
	
	continueAddHistoryItemToEditor : function (argArray) {
		var theElement = argArray[0];
		var type = argArray[1];
		
		var thePostID = theElement.getAttribute("postid");
		var theDateCreated = theElement.getAttribute("date");
	
		var db = SCRIBEFIRE_DB.getDB();
		
		try {
			var select = db.createStatement("SELECT content, title, categories, tags, isDraft, dateCreated, slug, custom_fields FROM "+SCRIBEFIRE_DB.historyTable+" WHERE type=?1 AND postId=?2 AND blogId=?3 LIMIT 1");
			select.bindStringParameter(0, type);
			select.bindStringParameter(1, thePostID);
			select.bindInt32Parameter(2, SCRIBEFIRE_BLOG.id);
			
			try {
				while (select.executeStep()) {
					var theContent = select.getUTF8String(0);
					theContent = theContent.replace(/&lt;/gi, "<");
					theContent = theContent.replace(/&gt;/gi, ">");
					theContent = theContent.replace(/<!--\s*more\s*-->/g, '<hr class="jump" />' + "\n\n");
					performancingEditor.setContent(theContent);

					var theTitle = select.getUTF8String(1);
					performancingEditor.setTitle(theTitle);

					var categories = select.getUTF8String(2).replace(/^\s+|\s+$/g, "");
					
					var tempArray = [];
					
					if (categories) {
						tempArray = categories.split(",");
					}
					
					if (tempArray.length == 0) {
						if (SCRIBEFIRE_BLOG.serviceObject.getPostCategories) {
							var myResponse = SCRIBEFIRE_BLOG.serviceObject.getPostCategories(thePostID);
							performancing_xmlcall.sendCommand(SCRIBEFIRE_BLOG.api, myResponse, 'postcategories', "", "", performancing_xmlcall.processData);
						}
					}
					
					gPerformancingUtil.setCategoriesSidebar(tempArray, false, true);

					var tags = select.getUTF8String(3);
					document.getElementById("input-tags").value = tags;
					performancingEditor.setTags(tags);
					
					var isDraft = (select.getInt32(4)) ? true : false;
					performancingEditor.setEntryIsDraft(isDraft);
					
					var dateCreated = select.getInt64(5);
					dateCreated = new Date(dateCreated);
                    
                    var slug = select.getUTF8String(6);
                    performancingEditor.setSlug(slug)
                    
					var custom_fields = select.getUTF8String(7);
					
					try {
						custom_fields = JSON.parse(custom_fields);
					} catch (e) {
						custom_fields = [];
						alert(e);
					}
					
					performancingEditor.setCustomFields(custom_fields);
					
					performancingEditor.setOriginalDateCreated(dateCreated);

					performancingEditor.setEntryType(type);
					performancingEditor.setEntryId(thePostID);

					break;
				}
			} catch (e) {
				SFLOG(e);
			} finally {
				select.reset();
			}
		}catch(e){
			SFLOG(e);
		}
		
		SCRIBEFIRE_DB.closeDB(db);
	}
};